<template>
    <div>
        <div>
            <div style="display: flex;justify-content: space-between">
                <div>
                    <el-input placeholder="请输入设备名称进行搜索，可以直接回车搜索..." prefix-icon="el-icon-search"
                              clearable
                              @clear="initRepair"
                              style="width: 350px;margin-right: 10px" v-model="keyword"
                              @keydown.enter.native="initRepair" :disabled="showAdvanceSearchView"></el-input>
                    <el-button icon="el-icon-search" type="primary" @click="initRepair" :disabled="showAdvanceSearchView">
                        搜索
                    </el-button>
                    <el-button type="primary" @click="showAdvanceSearchView = !showAdvanceSearchView">
                        <i :class="showAdvanceSearchView?'fa fa-angle-double-up':'fa fa-angle-double-down'"
                           aria-hidden="true"></i>
                        高级搜索
                    </el-button>
                </div>
                <div>
                    <el-button type="primary" icon="el-icon-plus" @click="showAddRepairView">
                        添加维修清单
                    </el-button>
                </div>
            </div>
            <transition name="slide-fade">
                <div v-show="showAdvanceSearchView"
                     style="border: 1px solid #409eff;border-radius: 5px;box-sizing: border-box;padding: 5px;margin: 10px 0px;">
                    <el-row style="margin-top: 10px">
                        <el-col :span="5">
                           设备类型:
                            <el-select style="width: 170px" v-model="repair.equitType" placeholder="请选择">
                                <el-option
                                        v-for="item in options"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                </el-option>
                            </el-select>
                        </el-col>
                        <el-col :span="5" :offset="4">
                            <el-button size="mini">取消</el-button>
                            <el-button size="mini" icon="el-icon-search" type="primary" @click="initRepair('advanced')">搜索</el-button>
                        </el-col>
                    </el-row>
                </div>
            </transition>
        </div>
        <div style="margin-top: 10px">
            <el-table
                    size="mini"
                    :data="repairs"
                    stripe
                    border
                    v-loading="loading"
                    element-loading-text="正在加载..."
                    element-loading-spinner="el-icon-loading"
                    element-loading-background="rgba(0, 0, 0, 0.8)"
                    style="width: 100%">
                <el-table-column
                        align="center"
                        type="selection"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="equitId"
                        label="设备编号"
                        fixed
                        align="center"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="equitName"
                        align="center"
                        label="设备名称"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="equitType"
                        label="设备类型"
                        align="center"
                        width="120">
                </el-table-column>
                <el-table-column
                        prop="matterDesc"
                        width="260"
                        align="center"
                        label="设备情况描述">
                </el-table-column>
                <el-table-column
                        prop="repairName"
                        width="120"
                        align="center"
                        label="报修申请人">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        width="120"
                        align="center"
                        label="创建时间">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        width="120"
                        align="center"
                        label="操作">
                    <template slot-scope="scope">
                        <el-button @click="showEditRgistryView(scope.row)" style="padding: 3px" type="primary" size="normal">编辑</el-button>
                        <el-button @click="deleteRepair(scope.row)" style="padding: 3px" size="normal" type="danger">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div style="display: flex;justify-content: flex-end">
                <el-pagination
                        background
                        @current-change="currentChange"
                        @size-change="sizeChange"
                        layout="sizes, prev, pager, next, jumper, ->, total, slot"
                        :total="total">
                </el-pagination>
            </div>
        </div>
        <el-dialog
                :title="title"
                :visible.sync="dialogVisible"
                width="80%">
            <div>
                <el-form style="margin-left: 30px" :rules="rules" ref="repairForm" :model="repair" label-width="80px">
                    <el-row style="margin-top: 20px;">
                        <el-col :span="6">
                            <el-form-item label="设备编号" prop="equitId">
                                <el-input class="input-style" placeholder="请输入设备编号" v-model="repair.equitId"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" style="margin-left: 40px">
                            <el-form-item label="设备名称" prop="equitName">
                                <el-input style="width: 200px" placeholder="请输入设备名称" v-model="repair.equitName"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" style="margin-left: 70px">
                            <el-form-item label="设备类型" prop="equitType">
                                <el-select style="width: 170px" v-model="repair.equitType" placeholder="请选择">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row style="margin-top: 40px;">
                        <el-col :span="6">
                            <el-form-item label="情况描述" prop="matterDesc">
                                <el-input type="textarea" style="width: 300px" v-model="repair.matterDesc"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" style="margin-left: 200px">
                            <el-form-item label="申请人" prop="repairName">
                                <el-input class="input-style" placeholder="请输入报修申请人" v-model="repair.repairName"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddRepair">确 定</el-button>
  </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "repairBasic",
        data() {
            return {
                fileList: [],
                searchValue: {
                    instituteId: null,
                },
                title: '',
                importDataBtnIcon: 'el-icon-upload2',
                importDataDisabled: false,
                showAdvanceSearchView: false,
                institutes: [],
                repairs: [],
                loading: false,
                popVisible: false,
                popVisible2: false,
                dialogVisible: false,
                total: 0,
                page: 1,
                keyword: '',
                size: 10,
                repair: {
                    equitId: "",
                    equitName: "",
                    equitType: "",
                    matterDesc: '',
                    repairName: "",
                    createDate: '',
                },
                rules: {
                    equitId: [{required: true, message: '请输入设备编号', trigger: 'blur'}],
                    equitName: [{required: true, message: '请输入设备名称', trigger: 'blur'}],
                    equitType: [{required: true, message: '请输入设备类型', trigger: 'blur'}],
                    matterDesc: [{required: true, message: '请输入设备描述', trigger: 'blur'}],
                    repairName: [{required: true, message: '请输入报修申请人', trigger: 'blur'}],
                },
                options: [{
                    value: '选项1',
                    label: '电子'
                }, {
                    value: '选项2',
                    label: '与网络相关'
                }, {
                    value: '选项3',
                    label: '配件'
                }, {
                    value: '选项4',
                    label: '与生命相关'
                }],
                value: ''
            }
        },
        mounted() {
            this.initRepair();
        },
        methods: {
            searchViewHandleNodeClick(data) {
                this.searchValue.instituteId = data.id;
                this.popVisible2 = !this.popVisible2
            },
            showEditRgistryView(data) {
                this.title = '编辑维修清单信息';
                this.repair = data;
                this.dialogVisible = true;
            },
            deleteRepair(data) {
                this.$confirm('此操作将永久删除【' + data.equitName + '】, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest("/assist/repair/" + data.id).then(res => {
                        if (res) {
                            this.initRepair();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            doAddRepair() {
                if (this.repair.id) {
                    this.$refs['repairForm'].validate(valid => {
                        if (valid) {
                            this.putRequest("/assist/repair/", this.repair).then(res => {
                                if (res) {
                                    this.dialogVisible = false;
                                    this.initRepair();
                                }
                            })
                        }
                    });
                } else {
                    this.$refs['repairForm'].validate(valid => {
                        if (valid) {
                            this.postRequest("/assist/repair/", this.repair).then(res => {
                                if (res) {
                                    this.dialogVisible = false;
                                    this.initRepair();
                                }
                            })
                        }
                    });
                }
            },
            sizeChange(currentSize) {
                this.size = currentSize;
                this.initRepair();
            },
            currentChange(currentPage) {
                this.page = currentPage;
                this.initRepair();
            },
            showAddRepairView() {
                this.title = '添加维修清单信息';
                this.dialogVisible = true;
            },
            initRepair(type) {
                this.loading = true;
                let url = '/assist/repair/?page=' + this.page + '&size=' + this.size;
                if (type && type === 'advanced') {
                    url += "&equitName=" + this.keyword;
                } else {
                    url += "&equitName=" + this.keyword;
                    console.log(url)
                }
                this.getRequest(url).then(res => {
                    this.loading = false;
                    if (res) {
                        this.repairs = res.data;
                        this.total = res.total;
                    }
                });
            }
        }
    }
</script>

<style>
    /* 可以设置不同的进入和离开动画 */
    /* 设置持续时间和动画函数 */
    .slide-fade-enter-active {
        transition: all .8s ease;
    }

    .slide-fade-leave-active {
        transition: all .8s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }

    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */
    {
        transform: translateX(10px);
        opacity: 0;
    }

    #el-upload-style{
        height: 36px;
    }
    .upload-demo{
        float: left;
        margin-left: 300px;
        margin-top: 10px;
    }
</style>
